package tech.aistar.Conteoller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.aistar.mapper.JobMapper;
import tech.aistar.mapper.NotlikeMapper;
import tech.aistar.model.dto.AllJobCompanyDTO;
import tech.aistar.model.dto.Code;
import tech.aistar.model.dto.Result;
import tech.aistar.model.entity.*;
import tech.aistar.service.ICompanyService;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("notlike")
public class NotLikeController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private NotlikeMapper notlikeMapper;

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private ICompanyService iCompanyService;


    @GetMapping("add")
    public Result addNotLike(String jId){
        Integer jobId= Integer.valueOf(jId);
        Customer customer= (Customer) redisTemplate.opsForValue().get("customer");
        Notlike notlike=new Notlike();
        notlike.setCustomerId(customer.getId());
        notlike.setJobId(jobId);
        int i = notlikeMapper.insertNotLike(notlike);
        if (i==1)
        {
            return new Result(Code.OK,"插入成功");
        }

        return new Result(Code.BAD,"插入失败");

    }


    @GetMapping("del")
    public Result delNotLike(String jId){
        Integer jobId= Integer.valueOf(jId);
        Customer customer= (Customer) redisTemplate.opsForValue().get("customer");
        int i = notlikeMapper.delNotLike(customer.getId(), jobId);
        if (i==1)
        {
            return new Result(Code.OK,"取消成功");
        }

        return new Result(Code.BAD,"取消失败");

    }

    @GetMapping("sel")
    public Result getAll(){
        Customer customer= (Customer) redisTemplate.opsForValue().get("customer");
        List<Notlike> notlikes=notlikeMapper.getNotLikeByCID(customer.getId());
        List<Integer> ids=new ArrayList<>();
        for (int i=0;i<notlikes.size();i++){
            ids.add(notlikes.get(i).getJobId());
        }
        List<Job> jobs=jobMapper.getByIds(ids);
        List<Company> companies=iCompanyService.findAllCompany();
        AllJobCompanyDTO allJobCompanyDTO=new AllJobCompanyDTO();
        allJobCompanyDTO.setJobList(jobs);
        allJobCompanyDTO.setCompanyList(companies);
        return new Result(Code.OK,"查找成功",allJobCompanyDTO);
    }
}
